package jus.aor.talk.v3;

import java.io.PrintWriter;
import java.net.*;
/**
* @author Philippe Morat
* @email Philippe.Morat@imag.fr
*/
public class RemoteApplicationLoader extends java.net.URLClassLoader {
	/** le mouchard d execution*/
	PrintWriter pw= new PrintWriter(System.out) ;
	
	/**
	* @param urls Le tableau des Urls de codebase.
	* @param pw le mouchard de service
	*/
	public RemoteApplicationLoader(URL[] urls, PrintWriter pw) {
		super(urls);
		this.pw=pw;
	}
	/**
	* @param urls Le tableau des Urls de codebase.
	*/
	public RemoteApplicationLoader(URL[] urls) {
		super(urls);
	}
	/* (non-Javadoc)
	* @see java.lang.ClassLoader#findClass(java.lang.String)
	*/
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		// COMPLETER
		try {
			pw.println("Remote Application Loader -> Searching Class " + name);
			Class<?> loadedClass = super.findClass(name);
			pw.println("Remote Application Loader -> Class " + name + " found."); 
			return loadedClass;
		}
		catch(ClassNotFoundException e) {
			pw.println("Remote Application Loader -> Class " + name + " not found (ClassNotFoundException caught).");
			throw e;
		}
	}
	/* (non-Javadoc)
	* @see java.lang.URLClassLoader#loadClass(java.lang.String,boolean)
	*/
	public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
		// COMPLETER
		try {
			pw.println("Remote Application Loader -> loading Class " + name);
			Class<?> loadedClass = super.loadClass(name, resolve);
			pw.println("Remote Application Loader -> Class " + name + " loaded."); 
			return loadedClass;
		}
		catch(ClassNotFoundException e) {
			pw.println("Remote Application Loader -> Class " + name + " not loaded (ClassNotFoundException caught).");
			throw e;
		}
	}
	/* (non-Javadoc)
	* @see java.lang.Object#toString()
	*/
	public String toString(){
		return getClass().getName();
	}
	
	public void printWriter(PrintWriter pw) {
		this.pw = pw;
	}
	//*
	public void addURL(URL url) {
		pw.println("Remote Application Loader -> URL ajoutee : " + url);pw.flush();
		super.addURL(url);
	}//*/
}